//
//  WBVisitorLoginView.swift
//  Weibo
//
//  Created by xu on 2017/3/19.
//  Copyright © 2017年 xuantong. All rights reserved.
//

import UIKit
import SnapKit
//定义代理
@objc protocol WBVisitorLoginViewDelegate:NSObjectProtocol {
    
    //在协议中定义方法的声明
    @objc optional func userWillLogin()
    
    //用户将要注册
     func userWillRegister()

    
}
class WBVisitorLoginView: UIView {
 
    //声明代理属性
    weak var delegate :WBVisitorLoginViewDelegate?
    
    
    //MARK: 外面设置信息的方法
    func updateInfo(title:String,imageName: String,isHome: Bool = false){
       
        self.tipLabel.text = title
       
        self.circleView.image = UIImage(named: imageName)
        
        if isHome {
            //执行动画
            startAnimation()
        }else{
    
            //不是首页
            self.largeHouse.isHidden = true
            //隐藏遮罩
            self.coverView.isHidden = true

        }
    }
    
    
    //添加动画
    private func startAnimation() {
        
        //创建动画对象
        
        let animator = CABasicAnimation(keyPath:"transform.rotation");
        
        //设置属性
        //动画次数
        animator.repeatCount = MAXFLOAT

        //动画时间
        animator.duration = 12
        
       
        //动画弧度
        animator.toValue = M_PI * 2
       
        //isRemovedOnCompletion 动画执行完,会默认将动画移除,设置为false 就不会移除了
        animator.isRemovedOnCompletion = false
       
        //添加layer上
        circleView.layer.add(animator, forKey: nil)
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()

        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    
    //MARK: 根据子页面显示逻辑设置
    
    ///MARK: 搭建界面
     private func setupUI(){
        
      
        
        addSubview(circleView)
        self.backgroundColor = UIColor (colorLiteralRed: 237/255, green: 237/255, blue: 237/255, alpha: 0);
        
        
        addSubview(circleView)
        addSubview(coverView)
        addSubview(largeHouse)
        addSubview(tipLabel)
        addSubview(loginButton)
        addSubview(registerBtn)
        
        
        //约束
        circleView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-80)
            
        }
        
        //房子约束
        largeHouse.snp.makeConstraints { (make) in
            make.center.equalTo(circleView)

        }
        
        //文字约束
        tipLabel.snp.makeConstraints { (make) in
            make.top.equalTo(circleView.snp.bottom).offset(20)
            make.centerX.equalTo(circleView)
                    }
        //登录按钮约束
        loginButton.snp.makeConstraints { (make) in
            make.left.equalTo(tipLabel)
            make.top.equalTo(tipLabel.snp.bottom).offset(20)
            make.width.equalTo(100)
        }
        
        //注销按钮约束
        registerBtn.snp.makeConstraints { (make) in
            make.right.equalTo(tipLabel)
            make.top.equalTo(tipLabel.snp.bottom).offset(20)
            make.width.equalTo(100)
        }
        
        
        coverView.snp.makeConstraints { (make) in
            make.center.equalTo(circleView)
        }
    
        
        //设置背景颜色
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)

    }
    
    
    
    
    
    ///MARK: 圈圈
    private lazy var circleView:UIImageView = UIImageView(image:#imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    
    //MARK: 大房子
    
    private lazy var largeHouse:UIImageView = UIImageView(image:#imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    
    //遮罩
    private lazy var coverView:UIImageView = UIImageView(image:#imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    
   
    
    
    //MARK: 提示
    private lazy var tipLabel: UILabel = {
        
        let label = UILabel(title: "关注一些人,关注一些人,回到这里看看有什么惊喜", fontSize: 14, textColor: UIColor.darkGray)
        
        label.numberOfLines = 0
        
        label.textAlignment = .center
        
        //给一个最大(预估)的宽度
        label.preferredMaxLayoutWidth = 220
        
        
        
        return label
    }()

   
    //登录按钮
//    private lazy var loginButton :UIButton = UIButton(title: "登录", fontSize: 14, textColor: UIColor.darkGray)
    
        lazy var loginButton :UIButton = {
       
        let button = UIButton()
        
        button.setTitle("登录", for: .normal)
        
        button.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        
        button .setTitleColor(UIColor .darkGray, for: .normal)
        
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        return button
        
    }()
    
    
    //注册
    
      lazy var  registerBtn:UIButton = {
       
        let button = UIButton()
        
        button.setTitle("注销", for: .normal)
        
        button.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        
        button .setTitleColor(UIColor .darkGray, for: .normal)
        
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        return button

        
    }()
    
    
   
    
    @objc private func loginBtnDidClick() {
        //调用代理方法 可选的协议方法
        delegate?.userWillLogin?()
    }
    
    @objc private func registerBtnDidClick() {
        //必选的协议方法
        delegate?.userWillRegister()
    }

    
   
    
}
